<?php 
/**
 *  Por Federico Pisarello - SisDev Software (c) 2012 - Buenos Aires, Argentina.
 * 	fpisarello@sisdevsoft.com
 */

require("includes/funciones.inc.php");

session_start();
if(!isset($_SESSION['idUser']))
	exit();

function searchABMPhrases()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");
	require("includes/datatables_db/Datatables.php");
	
	$datatables = new Datatables();
	
	$datatables
			->select('idPhrase, description, language')
			->from('phrases');

	return $datatables->generate();	
}

function addOrEdit()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");
	
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;

	$DB->StartTrans(); // start transaction
		
	if(!isset($_REQUEST["idPhrase"])){			
		$strSQL = "INSERT INTO phrases ( ";
		$strSQL .= "	description, ";
		$strSQL .= "	state, ";
		$strSQL .= "	language ";
		$strSQL .= "	) VALUES ( ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["description"], "UTF-8")."', ";
		$strSQL .= "	'".$stateActive."', ";
		$strSQL .= "	'".$_REQUEST["cmbLanguage"]."' ";
		$strSQL .= "	)";
		
		$DB->Execute($strSQL);
	}
	else{
		$strSQL = "UPDATE phrases SET ";
		$strSQL .= "	description = '".mb_strtoupper($_REQUEST["description"], "UTF-8")."', ";
		$strSQL .= "	state = '".$stateModif."',";
		$strSQL .= "	language = '".$_REQUEST["cmbTypeUser"]."' ";
		$strSQL .= " WHERE idPhrase = ". $_REQUEST["idPhrase"];
		
		$DB->Execute($strSQL);
	}
	
	if (!$DB->CompleteTrans())
	{
		$arrJSON->status = "ERROR";
		$arrJSON->msg = $a_languages['Common.ErrorGrabar'];
	}
	else
	{
		$arrJSON->status = "OK";
		$arrJSON->msg = $a_languages['Common.OkGrabar'];
	}	
	return json_encode($arrJSON);
}

function editPhrase()
{
	require("includes/constants.php");
	
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$rsPhrase = $DB->Execute("SELECT * FROM phrases WHERE idPhrase = " . $_REQUEST["idPhrase"]);
						
	if(!$rsPhrase->EOF){
		$joPhrase->data['description'] = $rsPhrase->fields("description");
		$joPhrase->data['language'] = $rsPhrase->fields("language");
		
		return json_encode($joPhrase->data);
	}
	else
		return json_encode(array());
}

switch($_REQUEST['actionOfForm'])
{
	case "EDIT":
		echo editPhrase();
		break;
	case "search":
		echo searchABMPhrases();
		break;
	case "addOrEdit":
		echo addOrEdit();
		break;		
}
?>
